#include <iostream>
using namespace std;
//多态
#if 0
class Animal
{
public:
    virtual void talk() const {}
};
class Dog : public Animal
{
public:
    virtual void talk() const {cout << "汪汪" << endl;}
};
class Cat : public Animal
{
public:
    virtual void talk() const {cout << "喵喵" << endl;}
};
void Func(const Animal& d)
{
    d.talk();
}
void test1()
{
    Dog d;
    Cat c;
    Func(c);
    Func(d);
}
#endif
//协变
#if 0
class A{};
class B : public A{};

class Person
{
public:
    virtual A* func()
    {
        cout << "买票->全价" << endl;
    }
};
class Student : public Person
{
public:
    virtual B* func()
    {
        cout << "买票->半价" << endl;
    }
};
void test2()
{
    Person* p = new Person;
    Person* s = new Student;
    p->func();
    s->func();
}
#endif

//析构函数
#if 0
class A
{
public:
    virtual ~A()
    {
        cout << "~A()" << endl;
    }
};
class B : public A
{
public:
    ~B()
    {
        cout << "operator delete:" << _arr << endl;
        delete _arr;
    }
private:
    int* _arr = new int[10];
};
void test3()
{
    A* p1 = new A;
    A* p2 = new B;
    delete p1;
    delete p2;
}
#endif
#if 0
class Animal
{
public:
    virtual void cry(){}
private:
};
class Dog : public Animal
{
public:
    virtual void cry()
    {
        cout << "汪汪" << endl;
    }
};
class Cat : public Animal
{
public:
    virtual void cry()
    {
        cout << "喵喵" << endl;
    }
};
//非成员函数不能加virtual修饰
//virtual void func(){}
void test4()
{
    Dog dd;
    Cat cc;
    Animal& d = dd;
    Animal& c = cc;
    c.cry();
    d.cry();
}
#endif
//协变
#if 0
class A{};
class B :public A{};
class C :public A{};
class Animal
{
public:
    virtual Animal* cry(){}
private:
};
class Dog : public Animal
{
public:
    virtual Dog* cry()
    {
        cout << "汪汪" << endl;
    }
};
class Cat : public Animal
{
public:
    virtual Cat* cry()
    {
        cout << "喵喵" << endl;
    }
};
void test5()
{
    Dog dd;
    Cat cc;
    Animal& d = dd;
    Animal& c = cc;
    d.cry();
    c.cry();
}
#endif
#if 0
class A
{ 
public: 
    void test(float a) 
    { 
        cout << a; 
    } 
}; 
class B :public A
{ 
public: 
    void test(int b)
    { 
        cout << b; 
    } 
}; 
int main() 
{
    A *a = new A;
    B *b = new B;
    a = b;
    a->test(1.1); 
    return 0;
}
#endif
#if 0
class A
{
public:
    A ()
        :m_iVal(0)
    {
        test();
    }
    virtual void func() 
    {
        std::cout<<m_iVal<<' ';
    }
    void test()
    {
        func();
    }
public:
    int m_iVal;
};
class B : public A
{
public:
    B()
    {
        test();
    }
    virtual void func()
    {
        ++m_iVal;
        std::cout<<m_iVal<<' ';
    }
};
int main(int argc ,char* argv[])
{
    A*p = new B;
    p->test();
    return 0;
}
#endif
//析构函数的重写
#if 0
class A
{
public:
    virtual ~A()
    {
        cout << "~A()" << endl;
    }
};
class B : public A
{
public:
    ~B()
    {
        cout << "~B() -> delete:" << _ptr << endl;
        delete[] _ptr;
    }
private:
    int* _ptr = new int[10];
};
void test6()
{
    A* a = new A;
    A* b = new B;
    delete a;
    delete b;
}
#endif
#if 0
//override
class A
{
public:
    virtual ~A()
    {
        cout << "~A()" << endl;
    }
};
class B : public A
{
public:
    ~B() override
    {
        cout << "~B() -> delete:" << _ptr << endl;
        delete[] _ptr;
    }
private:
    int* _ptr = new int[10];
};
void test7()
{
    A* a = new A;
    A* b = new B;
    delete a;
    delete b;
}
#endif
#if 0
class Car
{
public:
    virtual void Drive() final {}
};
class Benz : public Car
{
    //final修饰的函数无法被重写
    // virtual void Drive()
    // {
    //     cout << "舒适" << endl;
    // }
};
void test8()
{
    Car c;
}
#endif
//纯虚函数和抽象类
class Car
{
public:
    virtual void Drive() = 0;
};
class Benz : public Car
{
public:
    void Drive() override
    {
        cout << "舒适" << endl;
    }
};
class BMW : public Car
{
public:
    void Drive() override
    {
        cout << "操控" << endl;
    }
};
void test9()
{
    Car* pBenz = new Benz;
    Car* pBMW = new BMW;
    pBenz->Drive();
    pBMW->Drive();
}
int main()
{
    test9();
	return 0;
}
/*
2025.1.17 假期补课打卡第八天 多态
多态通俗来讲就是多种形态,多态可以分为编译时多态(静态多态),
和运行时多态(动态多态),静态多态主要就是函数重载和函数模板,
运行时多态,具体点讲就是去完成某个行为,传不同的对象就可以
达到不同的行为,,就达到多种形态,就比如买票,普通人是全价,
学生会打折,军人会优先,这就是多个对象可以达到多种形态。
笔记:https://blog.csdn.net/m0_74271757/article/details/143473277?spm=1001.2014.3001.5501
*/